package com.cloud.code.真题;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 约瑟夫问题
 */
public class Demo12 {
    public static void main(String[] args) {
        int[] nums = new int[]{3, 1, 2, 4};
        int len = 7 - 1;

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            list.add(nums[i]);
        }
        List<Integer> res = new LinkedList<>();
        int cur = 0;
        while (list.size() > 0) {
            while (len != 0) {
                len--;
                cur++;
                if (cur >= list.size()) {
                    cur = 0;
                }
            }
            Integer integer = list.get(cur);
            len = integer - 1;
            res.add(integer);
            list.remove(cur);
        }

        System.out.println(res);
    }
}
